package com.example.demo.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Choisaaaa on 2018/6/7.
 * 全局异常处理类
 * 步骤
 * 1.新建一个全局异常处理类：GlobalDefaultExceptionHandler
 * 2.类上添加注解:@ControllerAdvice
 * 3.编写方法
 * 4.方法上添加@ExceptionHandler注解拦截相应的异常信息
 * 5.如果返回的是String或者Json数据，那么还需要在方法上添加注解@ResponseBody
 * 6.如果返回类型是ModelAndView
 */

@ControllerAdvice
public class GlobalDefaultExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public String defaultExceptionHandler(HttpServletRequest request,Exception e){
        return "对不起，服务器繁忙，请稍后再试";
    }
}
